package com.guapi.exception;

import com.guapi.dao.ResponseData;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice(annotations = {Controller.class, Aspect.class})
public class DefaultExceptionHandler {

    @ExceptionHandler(Throwable.class)
    public ResponseData defaultExceptionHandler(Throwable throwable){
        throwable.printStackTrace();
        ResponseData responseData = new ResponseData();
        responseData.setSuccess(false);
        if (!(throwable instanceof SelfException)){
            //SelfException selfException = new SelfException(throwable);
            responseData.setMessage(throwable.getMessage());
            responseData.setCode("500");
        }else{
            //System.out.println(throwable.getMessage());
            responseData.setMessage(throwable.getMessage());
            responseData.setCode(((SelfException) throwable).getCode()==null?"500":((SelfException) throwable).getCode());
        }
        return responseData;
    }
}
